/*
 * Copyright 2013 Glassy Cinema.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package hu.glassy.web.panel.movie.edit;

import hu.glassy.core.model.Movie;
import hu.glassy.core.service.CodeService;
import hu.glassy.core.service.MovieService;
import hu.glassy.web.component.common.CustomAjaxSubmitLink;
import hu.glassy.web.component.feedback.CustomFeedbackPanel;
import hu.glassy.web.component.location.CountrySelect;
import hu.glassy.web.component.visitor.ErrorMessageFormVisitor;
import hu.glassy.web.panel.common.AdminPanel;
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
import org.apache.wicket.datetime.StyleDateConverter;
import org.apache.wicket.datetime.markup.html.form.DateTextField;
import org.apache.wicket.extensions.yui.calendar.DatePicker;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.NumberTextField;
import org.apache.wicket.markup.html.form.StatelessForm;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.apache.wicket.validation.validator.StringValidator;

/**
 *
 * @author Alexander Molnár <molnar.alexander@sonrisa.hu>
 */
public final class MovieDataEditPanel extends AdminPanel<Movie>
{

  @SpringBean
  private MovieService movieService;

  @SpringBean
  CodeService codeService;

  /**
   * Standard constructor
   *
   * @param id Component ID
   */
  public MovieDataEditPanel(String id)
  {
    this(id, Model.of(new Movie()));
  }

  /**
   * Constructor
   *
   * @param id    Component ID
   * @param model The model of the movie to edit
   */
  public MovieDataEditPanel(String id, IModel<Movie> model)
  {
    super(id, model);
    setDefaultModel(new CompoundPropertyModel<Movie>(model));

    // Create the form
    final Form<Movie> form = new StatelessForm<Movie>("movieEditForm")
    {

      @Override
      protected void onSubmit()
      {
        super.onSubmit();

        movieService.saveOrUpdate(MovieDataEditPanel.this.getMainObject());
        success(getString("movie.edit.save.success"));
      }

    };

    // Add feedback panel
    FeedbackPanel feedbackPanel = new CustomFeedbackPanel("feedbackPanel", form);
    form.add(feedbackPanel);

    // Display Name field
    TextField<String> titleInput = new TextField<String>("title");
    titleInput.setRequired(Movie.TITLE_REQUIRED);
    titleInput.add(StringValidator.maximumLength(Movie.TITLE_LENGTH));
    form.add(titleInput);

    // Short description
    TextArea<String> shortDescriptionInput = new TextArea<String>("shortDescription");
    shortDescriptionInput.setRequired(Movie.SHORT_DESCRIPTION_REQUIRED);
    shortDescriptionInput.add(StringValidator.maximumLength(Movie.SHORT_DESCRIPTION_LENGTH));
    form.add(shortDescriptionInput);

    // Long description
    TextArea<String> longDescriptionInput = new TextArea<String>("longDescription");
    longDescriptionInput.setRequired(Movie.LONG_DESCRIPTION_REQUIRED);
    longDescriptionInput.add(StringValidator.maximumLength(Movie.LONG_DESCRIPTION_LENGTH));
    form.add(longDescriptionInput);

    // Total length field
    NumberTextField<Integer> totalLengthInput = new NumberTextField<Integer>("totalLength");
    totalLengthInput.setRequired(Movie.TOTAL_LENGTH_REQUIRED);
    totalLengthInput.setMinimum(0);
    form.add(totalLengthInput);

    // Release date input
    DateTextField releaseDateInput = new DateTextField("releaseDate", new StyleDateConverter(true));
    releaseDateInput.setRequired(Movie.RELEASE_DATE_REQUIRED);
    form.add(releaseDateInput);

    // Date picker for Date input
    DatePicker datePicker = new DatePicker();
    datePicker.setShowOnFieldClick(true);
    datePicker.setAutoHide(true);
    releaseDateInput.add(datePicker);

    // Select release country
    CountrySelect releaseCountryInput = new CountrySelect("releaseCountry");
    releaseCountryInput.setRequired(Movie.RELEASE_COUNTRY_REQUIRED);
    form.add(releaseCountryInput);

    // Add submit button
    AjaxSubmitLink submitLink = new CustomAjaxSubmitLink("submitButton");
    form.add(submitLink);

    form.visitChildren(new ErrorMessageFormVisitor<String>());
    add(form);
  }
}
